package com.Global;

import android.annotation.SuppressLint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Global {
	public static String UserName;
	public static String ServerName;
	public static String ServerPort;
	public static String Login;
	public static String Password;
	public static String DatabaseName;
	public static String ChiNhanh;
	public static String StatusServerName;

	public static ResultSet excuteQuerySQLServer(String strQuery,
			String strServerName, String strServerPort, String strLogin,
			String strPassword, String strDatabaseName) {
		Connection conn = null;
		try {
			String driver = "net.sourceforge.jtds.jdbc.Driver";
			Class.forName(driver).newInstance();

			String url = "jdbc:jtds:sqlserver://" + strServerName + ":"
					+ strServerPort + ";DatabaseName=" + strDatabaseName
					+ ";integratedSecurity=true;user=" + strLogin
					+ ";password=" + strPassword + "";
			conn = DriverManager.getConnection(url);
			PreparedStatement stmt = conn.prepareStatement(strQuery);
			stmt.execute();
			ResultSet rs = stmt.getResultSet();
			// stmt.close();
			// conn.close();
			return rs;
		} catch (Exception e) {
			return null;
		}
	}

	public static ResultSet excuteQuerySQLServer(String strQuery) {
		String strServerName = Global.ServerName;
		String strServerPort = Global.ServerPort;
		String strDatabaseName = Global.DatabaseName;
		String strLogin = Global.Login;
		String strPassword = Global.Password;
		Connection conn = null;
		try {
			String driver = "net.sourceforge.jtds.jdbc.Driver";
			Class.forName(driver).newInstance();

			String url = "jdbc:jtds:sqlserver://" + strServerName + ":"
					+ strServerPort + ";DatabaseName=" + strDatabaseName
					+ ";integratedSecurity=true;user=" + strLogin
					+ ";password=" + strPassword + "";
			conn = DriverManager.getConnection(url);
			PreparedStatement stmt = conn.prepareStatement(strQuery);
			stmt.execute();
			ResultSet rs = stmt.getResultSet();
			// stmt.close();
			// conn.close();
			return rs;
		} catch (Exception e) {
			return null;
		}
	}

	public static void excuteUpdate(String strQuery) {
		String strServerName = Global.ServerName;
		String strServerPort = Global.ServerPort;
		String strDatabaseName = Global.DatabaseName;
		String strLogin = Global.Login;
		String strPassword = Global.Password;
		Connection conn = null;
		try {
			String driver = "net.sourceforge.jtds.jdbc.Driver";
			Class.forName(driver).newInstance();

			String url = "jdbc:jtds:sqlserver://" + strServerName + ":"
					+ strServerPort + ";DatabaseName=" + strDatabaseName
					+ ";integratedSecurity=true;user=" + strLogin
					+ ";password=" + strPassword + "";
			conn = DriverManager.getConnection(url);
			PreparedStatement stmt = conn.prepareStatement(strQuery);
			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String formatDecimalDetail(float number) {
		return formatCurrency(String.valueOf(number));
	}

	public static String formatDecimal(float number) {
//	Integer t =Math.round(number);
		number =Math.round(number)/1000;	
		formatCurrency(String.valueOf(number));
		return formatCurrency(String.valueOf(number));
	}

	// format Currrency ,delete .00
	public static String formatCurrency(String moneyString) {
		Float money = Float.valueOf(moneyString);
		return NumberFormat.getNumberInstance(Locale.GERMANY).format(money);
	}

	public static String formatNumberG(String strNumber) {
		try {
			Double dbNumber = Double.valueOf(strNumber);
			return NumberFormat.getNumberInstance(Locale.GERMANY)
					.format(dbNumber).toString();
		} catch (Exception e) {
			return strNumber;
		}
	}

	/**
	 * @author trangbm
	 * @param date
	 * @return Convert date by format DD/MM/YYYY date 20/03/2014
	 */
	@SuppressLint("SimpleDateFormat")
	public static String fomartDate(String date) {
		String convertDate = "";
		Timestamp tDate = Timestamp.valueOf(date);
		SimpleDateFormat dt1 = new SimpleDateFormat("dd/MM/yyyy");
		convertDate = dt1.format(tDate).substring(0, 10);
		return convertDate;
	}
	
	public static String formatDay(int day)
	{
		if(day < 10)
			return "0"+String.valueOf(day);
		else
			return String.valueOf(day);
	}
	
	public static Double convertMooneyByDouble(Double mooney) {
		Double tmp = (double) Math.round(mooney / 1000);
		return tmp;
	}
}
